{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cae2e00c",
   "metadata": {},
   "source": [
    "# faiss安装\n",
    "安装faiss-gpu或者faiss(cpu)\n",
    "版本 faiss-gpu==1.7.2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ebe494c",
   "metadata": {},
   "source": [
    "# 测试Faiss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "62dbc437",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引初始化成功： <faiss.swigfaiss_avx2.IndexFlatL2; proxy of <Swig Object of type 'faiss::IndexFlatL2 *' at 0x7f6578170ed0> >\n"
     ]
    }
   ],
   "source": [
    "import faiss\n",
    "\n",
    "# 测试基础索引功能\n",
    "dimension = 128  # 向量维度\n",
    "index = faiss.IndexFlatL2(dimension)  # 初始化 L2 距离索引\n",
    "print(\"索引初始化成功：\", index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ad271eb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入相关依赖\n",
    "from langchain_text_splitters import CharacterTextSplitter\n",
    "from langchain_community.document_loaders import CSVLoader\n",
    "from langchain_ollama import OllamaEmbeddings\n",
    "from langchain_community.vectorstores import Chroma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dc5cb76c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.定义文档\n",
    "from langchain_core.documents import Document\n",
    "\n",
    "raw_documents = [\n",
    "    # 水果类\n",
    "    Document(\n",
    "        page_content=\"芒果是漆树科芒果属热带水果，原产于印度半岛。果实多为肾形或椭圆形，果皮颜色从青绿色到深黄色不等，果肉细腻香甜，富含 β- 胡萝卜素和膳食纤维。除直接生食外，还可制作果汁、果酱、芒果糯米饭等美食。\",\n",
    "        metadata={\"source\": \"水果\", \"type\": \"热带植物\"},\n",
    "    ),\n",
    "    # 蔬菜类\n",
    "    Document(\n",
    "        page_content=\"西兰花是十字花科芸薹属蔬菜，起源于地中海东部。其食用部分为绿色的花球，由无数细小的花蕾聚合而成，口感脆嫩清香，富含维生素 C、叶酸和萝卜硫素。常见做法有清炒、焯水凉拌、搭配肉类烤制。\",\n",
    "        metadata={\"source\": \"蔬菜\", \"type\": \"花菜类植物\"},\n",
    "    ),\n",
    "    # 动物类（海洋哺乳动物）\n",
    "    Document(\n",
    "        page_content=\"海豚是齿鲸亚目海豚科哺乳动物，广泛分布于全球各大洋。它们具有极高的智商和复杂的社交行为，通过回声定位导航和捕猎，主要以鱼类、乌贼为食。海豚常被视为友好的象征，也是海洋公园中的热门动物。\",\n",
    "        metadata={\"source\": \"动物\", \"type\": \"海洋哺乳动物\"},\n",
    "    ),\n",
    "    # 动物类（啮齿类）\n",
    "    Document(\n",
    "        page_content=\"仓鼠是啮齿目仓鼠科小型哺乳动物，原产于亚洲和欧洲部分地区。体型圆润，尾巴短小，毛色有金棕、银白、三线等多种，性格温顺胆小，以谷物、种子、蔬菜为食。因饲养简便、外形可爱，成为广受欢迎的宠物。\",\n",
    "        metadata={\"source\": \"动物\", \"type\": \"啮齿类哺乳动物\"},\n",
    "    ),\n",
    "    # 生物类（灵长目）\n",
    "    Document(\n",
    "        page_content=\"黑猩猩是灵长目人科黑猩猩属动物，与人类亲缘关系最近，基因相似度高达 98% 以上。它们栖息在非洲热带雨林，过着群居生活，会使用工具获取食物（如用树枝钓白蚁），具有情感表达能力和学习行为。\",\n",
    "        metadata={\"source\": \"生物\", \"type\": \"非人灵长类\"},\n",
    "    ),\n",
    "    # 天文类\n",
    "    Document(\n",
    "        page_content=\"月球是地球唯一的天然卫星，直径约 3476 公里，距离地球约 38 万公里。它本身不发光，通过反射太阳光呈现不同月相，对地球的潮汐现象有显著影响。月球表面布满环形山、月海和月陆，是人类唯一登陆过的地外天体。\",\n",
    "        metadata={\"source\": \"天文\", \"type\": \"卫星\"},\n",
    "    ),\n",
    "    # 历史类\n",
    "    Document(\n",
    "        page_content=\"故宫博物院位于北京中轴线中心，是明清两代皇家宫殿，旧称紫禁城。始建于明永乐十八年，占地约 72 万平方米，拥有宫殿楼宇 9000 余间，珍藏文物 186 万余件。其建筑布局严谨对称，是中国古代宫殿建筑的巅峰之作，现为世界文化遗产。\",\n",
    "        metadata={\"source\": \"历史\", \"type\": \"皇家建筑\"},\n",
    "    ),\n",
    "    # 物理类\n",
    "    Document(\n",
    "        page_content=\"相对论是爱因斯坦提出的物理学理论，分为狭义相对论（1905 年）和广义相对论（1915 年）。狭义相对论提出光速不变原理和相对性原理，颠覆了经典力学的绝对时空观；广义相对论则将引力解释为时空弯曲的效应，成功预言了黑洞、引力透镜等现象。\",\n",
    "        metadata={\"source\": \"物理\", \"type\": \"基础科学\"},\n",
    "    ),\n",
    "    # 文学类\n",
    "    Document(\n",
    "        page_content=\"《三国演义》是中国古典文学四大名著之一，作者罗贯中。小说以东汉末年到西晋统一的历史为背景，讲述了魏、蜀、吴三国争霸的故事，塑造了诸葛亮、曹操、关羽等众多经典人物，兼具历史真实性与艺术感染力。\",\n",
    "        metadata={\"source\": \"文学\", \"type\": \"历史演义小说\"},\n",
    "    ),\n",
    "    # 医学类\n",
    "    Document(\n",
    "        page_content=\"流感病毒是正粘病毒科的 RNA 病毒，分为甲、乙、丙、丁四型，其中甲型流感病毒易发生变异。它主要通过飞沫和接触传播，症状包括高热、头痛、肌肉酸痛、呼吸道不适，易引发并发症。每年接种流感疫苗是最有效的预防手段。\",\n",
    "        metadata={\"source\": \"医学\", \"type\": \"呼吸道病毒\"},\n",
    "    ),\n",
    "    # 饮品类\n",
    "    Document(\n",
    "        page_content=\"普洱茶是中国云南特有的发酵茶，属于黑茶类。以云南大叶种晒青毛茶为原料，经渥堆发酵等工艺制成，茶汤色泽红浓透亮，滋味醇厚回甘，富含茶多酚和茶多糖。根据储存方式不同，可分为生普和熟普，具有越陈越香的特点。\",\n",
    "        metadata={\"source\": \"饮品\", \"type\": \"发酵茶\"},\n",
    "    ),\n",
    "    # 计算机类\n",
    "    Document(\n",
    "        page_content=\"Python 是一种解释型、面向对象的编程语言，由吉多・范罗苏姆于 1991 年发布。它语法简洁优雅，可读性强，拥有丰富的第三方库，广泛应用于数据分析、人工智能、Web 开发、自动化测试等领域，是编程入门的热门选择。\",\n",
    "        metadata={\"source\": \"计算机\", \"type\": \"编程语言\"},\n",
    "    ),\n",
    "    # 水生生物类（食用鱼类）\n",
    "    Document(\n",
    "        page_content=\"三文鱼是冷水性溯河洄游鱼类，背部呈青灰色，肉质橙红富含Omega-3，繁殖期会从海洋洄游至淡水河流产卵。\",\n",
    "        metadata={\"source\": \"水生生物\", \"type\": \"食用鱼类\"},\n",
    "    ),\n",
    "    # 水生生物类（无脊椎动物）\n",
    "    Document(\n",
    "        page_content=\"水母是海洋中的无脊椎动物，身体呈透明伞状，边缘有触手，部分种类能发光，通过喷水推进移动。\",\n",
    "        metadata={\"source\": \"水生生物\", \"type\": \"无脊椎动物\"},\n",
    "    ),\n",
    "    # 乐器\n",
    "    Document(\n",
    "        page_content=\"小提琴是一种弦乐器，有四根弦，通过弓摩擦弦发声，音色悠扬，是交响乐团的核心乐器之一。\",\n",
    "        metadata={\"source\": \"乐器\", \"type\": \"弦乐器\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"架子鼓是打击乐器的组合，包括军鼓、踩镲、嗵鼓等部件，常用于流行、摇滚等音乐类型的伴奏。\",\n",
    "        metadata={\"source\": \"乐器\", \"type\": \"打击乐器\"},\n",
    "    ),\n",
    "    # 交通工具\n",
    "    Document(\n",
    "        page_content=\"高铁是时速超过250公里的铁路列车，采用动车组技术，具有准时、舒适的特点，广泛用于城市间长途运输。\",\n",
    "        metadata={\"source\": \"交通工具\", \"type\": \"轨道交通\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"帆船依靠风力推动前进，船体轻盈，有单桅、双桅等类型，既是体育运动器材，也可用于休闲航行。\",\n",
    "        metadata={\"source\": \"交通工具\", \"type\": \"水上运输工具\"},\n",
    "    ),\n",
    "    # 航天器\n",
    "    Document(\n",
    "        page_content=\"空间站是在近地轨道运行的载人航天器，可长期驻留宇航员，用于开展空间科学实验和技术试验。\",\n",
    "        metadata={\"source\": \"航天器\", \"type\": \"载人航天器\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"卫星是环绕行星运行的人造天体，可用于通信、气象观测、导航等，按轨道分为地球同步卫星和极地卫星等。\",\n",
    "        metadata={\"source\": \"航天器\", \"type\": \"无人航天器\"},\n",
    "    ),\n",
    "    # 调料\n",
    "    Document(\n",
    "        page_content=\"肉桂是樟科植物的树皮，晒干后呈红棕色卷状，气味芳香，常用于烘焙、炖煮等料理的调味。\",\n",
    "        metadata={\"source\": \"调料\", \"type\": \"香辛料\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"酱油是大豆发酵制成的液体调味品，颜色深褐，咸鲜适口，是东亚烹饪中常用的基础调料。\",\n",
    "        metadata={\"source\": \"调料\", \"type\": \"发酵调味品\"},\n",
    "    ),\n",
    "    # 疾病\n",
    "    Document(\n",
    "        page_content=\"流感是由流感病毒引起的急性呼吸道传染病，症状包括发热、咳嗽、乏力，可通过飞沫传播。\",\n",
    "        metadata={\"source\": \"疾病\", \"type\": \"病毒性疾病\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"糖尿病是一种代谢性疾病，因胰岛素分泌不足或作用缺陷导致血糖升高，长期可能损害心脏、肾脏等器官。\",\n",
    "        metadata={\"source\": \"疾病\", \"type\": \"代谢性疾病\"},\n",
    "    ),\n",
    "    # 动物类\n",
    "    Document(\n",
    "        page_content=\"斑马是非洲草原特有的马科动物，黑白相间的条纹是其显著特征，这些条纹可能用于群体识别或迷惑捕食者。它们通常以草本植物为食，群居生活。\",\n",
    "        metadata={\"source\": \"野生动物\", \"type\": \"哺乳动物\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"鹦鹉是热带地区常见的鸟类，羽毛色彩艳丽，部分种类能模仿人类语言。它们主要以坚果、果实和种子为食，脚趾对生，适合抓握树枝。\",\n",
    "        metadata={\"source\": \"鸟类\", \"type\": \"宠物/野生动物\"},\n",
    "    ),\n",
    "    # 植物类\n",
    "    Document(\n",
    "        page_content=\"玫瑰是蔷薇科灌木，花瓣层层叠叠，花色丰富，常用于观赏和制作香水。其枝干有刺，喜阳光充足的环境，花期通常在春季至秋季。\",\n",
    "        metadata={\"source\": \"花卉\", \"type\": \"观赏植物\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"小麦是温带地区广泛种植的谷物，秸秆中空，穗状花序顶生，籽粒富含淀粉，是制作面粉的主要原料，可加工成面包、面条等食品。\",\n",
    "        metadata={\"source\": \"农作物\", \"type\": \"粮食植物\"},\n",
    "    ),\n",
    "    # 物品类\n",
    "    Document(\n",
    "        page_content=\"紫砂壶是中国特有的手工陶土茶具，以江苏宜兴产的最为著名，透气性好，适合冲泡乌龙茶，长期使用后壶身会形成包浆。\",\n",
    "        metadata={\"source\": \"工艺品\", \"type\": \"茶具\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"冲锋衣是户外爱好者常用的功能性服装，采用防水透气面料，接缝处有压胶处理，能抵御风雨，适合登山、徒步等场景。\",\n",
    "        metadata={\"source\": \"服饰\", \"type\": \"户外装备\"},\n",
    "    ),\n",
    "    # 自然现象类\n",
    "    Document(\n",
    "        page_content=\"极光多见于南北极地区的高纬度夜空，是太阳带电粒子与地球大气层中的气体碰撞产生的发光现象，颜色以绿色最为常见。\",\n",
    "        metadata={\"source\": \"自然现象\", \"type\": \"天文现象\"},\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"海啸是由海底地震、火山爆发或陨石撞击引发的巨型海浪，传播速度极快，到达岸边时会形成破坏性巨浪，对沿海地区造成严重影响。\",\n",
    "        metadata={\"source\": \"自然现象\", \"type\": \"地质灾害\"},\n",
    "    ),\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9074ac24",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 创建嵌入模型\n",
    "from langchain_ollama import OllamaEmbeddings\n",
    "embedding_model = OllamaEmbeddings(\n",
    "    base_url=\"http://host.docker.internal:11434\", # 仅仅因为我的环境在docker里，ollama装在windows系统里。\n",
    "    # base_url=\"http://localhost:11434\",\n",
    "    model=\"bge-m3:latest\"\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b8c454c",
   "metadata": {},
   "source": [
    "# 相似性搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "173ffb62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "结果 1:\n",
      "小麦是温带地区广泛种植的谷物，秸秆中空，穗状花序顶生，籽粒富含淀粉，是制作面粉的主要原料，可加工成面包、面条等食品。\n",
      "\n",
      "\n",
      "结果 2:\n",
      "高铁是时速超过250公里的铁路列车，采用动车组技术，具有准时、舒适的特点，广泛用于城市间长途运输。\n",
      "\n",
      "\n",
      "结果 3:\n",
      "Python 是一种解释型、面向对象的编程语言，由吉多・范罗苏姆于 1991 年发布。它语法简洁优雅，可读性强，拥有丰富的第三方库，广泛应用于数据分析、人工智能、Web 开发、自动化测试等领域，是编程入门的热门选择。\n",
      "\n",
      "\n",
      "结果 4:\n",
      "鹦鹉是热带地区常见的鸟类，羽毛色彩艳丽，部分种类能模仿人类语言。它们主要以坚果、果实和种子为食，脚趾对生，适合抓握树枝。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 4.创建向量数据库\n",
    "from langchain_community.vectorstores import FAISS\n",
    "db = FAISS.from_documents(\n",
    "    documents=raw_documents,\n",
    "    embedding=embedding_model,\n",
    ")\n",
    "# 5.获取检索器\n",
    "retriever = db.as_retriever(search_kwargs={\"k\": 4}) #这里设置返回的文档数\n",
    "docs = retriever.invoke(\"经济政策\")\n",
    "for i, doc in enumerate(docs):\n",
    "    print(f\"\\n结果 {i+1}:\\n{doc.page_content}\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e3429a1",
   "metadata": {},
   "source": [
    "# 简单过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "197472f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📌 内容: 海啸是由海底地震、火山爆发或陨石撞击引发的巨型海浪，传播速度极快，到达岸边时会形成破坏性巨浪，对沿海地区造成严重影响。\n"
     ]
    }
   ],
   "source": [
    "retriever = db.as_retriever(\n",
    "    search_kwargs={\"filter\": {\"source\": \"自然现象\"}}\n",
    ")\n",
    "docs = retriever.invoke(\"经济政策\")\n",
    "for doc in docs:\n",
    "    print(f\"📌 内容: {doc.page_content}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7661535f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📌 内容: 极光多见于南北极地区的高纬度夜空，是太阳带电粒子与地球大气层中的气体碰撞产生的发光现象，颜色以绿色最为常见。\n",
      "📌 内容: 海啸是由海底地震、火山爆发或陨石撞击引发的巨型海浪，传播速度极快，到达岸边时会形成破坏性巨浪，对沿海地区造成严重影响。\n"
     ]
    }
   ],
   "source": [
    "retriever = db.as_retriever(\n",
    "    search_kwargs={\"filter\": {\"source\": \"自然现象\"}}\n",
    ")\n",
    "docs = retriever.invoke(\"极光\")\n",
    "for doc in docs:\n",
    "    print(f\"📌 内容: {doc.page_content}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a65b6c5b",
   "metadata": {},
   "source": [
    "# 分数阈值查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c4586239",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📌 内容: 小麦是温带地区广泛种植的谷物，秸秆中空，穗状花序顶生，籽粒富含淀粉，是制作面粉的主要原料，可加工成面包、面条等食品。\n",
      "📌 内容: 高铁是时速超过250公里的铁路列车，采用动车组技术，具有准时、舒适的特点，广泛用于城市间长途运输。\n",
      "📌 内容: Python 是一种解释型、面向对象的编程语言，由吉多・范罗苏姆于 1991 年发布。它语法简洁优雅，可读性强，拥有丰富的第三方库，广泛应用于数据分析、人工智能、Web 开发、自动化测试等领域，是编程入门的热门选择。\n"
     ]
    }
   ],
   "source": [
    "retriever = db.as_retriever(\n",
    "    search_type=\"similarity_score_threshold\",\n",
    "    search_kwargs={\"score_threshold\": 0.1}\n",
    ")\n",
    "docs = retriever.invoke(\"经济政策\")\n",
    "for doc in docs:\n",
    "    print(f\"📌 内容: {doc.page_content}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4c2a39c",
   "metadata": {},
   "source": [
    "# 相关性分数查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "af97a5cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "相似度分数: 0.1296\n",
      "📌 内容: 小麦是温带地区广泛种植的谷物，秸秆中空，穗状花序顶生，籽粒富含淀粉，是制作面粉的主要原料，可加工成面包、面条等食品。\n",
      "\n",
      "相似度分数: 0.1093\n",
      "📌 内容: 高铁是时速超过250公里的铁路列车，采用动车组技术，具有准时、舒适的特点，广泛用于城市间长途运输。\n",
      "\n",
      "相似度分数: 0.1046\n",
      "📌 内容: Python 是一种解释型、面向对象的编程语言，由吉多・范罗苏姆于 1991 年发布。它语法简洁优雅，可读性强，拥有丰富的第三方库，广泛应用于数据分析、人工智能、Web 开发、自动化测试等领域，是编程入门的热门选择。\n",
      "\n",
      "相似度分数: 0.0665\n",
      "📌 内容: 鹦鹉是热带地区常见的鸟类，羽毛色彩艳丽，部分种类能模仿人类语言。它们主要以坚果、果实和种子为食，脚趾对生，适合抓握树枝。\n"
     ]
    }
   ],
   "source": [
    "# 注意只会返回满足阈值分数的文档，不会获取文档的得分。如果想查询文档的得分是否满足阈值，可以\n",
    "# 使用向量数据库的 similarity_search_with_relevance_scores 查看\n",
    "docs_with_scores = db.similarity_search_with_relevance_scores(\"经济政策\")\n",
    "for doc, score in docs_with_scores:\n",
    "    print(f\"\\n相似度分数: {score:.4f}\")\n",
    "    print(f\"📌 内容: {doc.page_content}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a1ad3e5",
   "metadata": {},
   "source": [
    "# MMR搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c5b63d6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "📌 内容: 小麦是温带地区广泛种植的谷物，秸秆中空，穗状花序顶生，籽粒富含淀粉，是制作面粉的主要原料，可加工成面包、面条等食品。\n",
      "📌 内容: 架子鼓是打击乐器的组合，包括军鼓、踩镲、嗵鼓等部件，常用于流行、摇滚等音乐类型的伴奏。\n",
      "📌 内容: 相对论是爱因斯坦提出的物理学理论，分为狭义相对论（1905 年）和广义相对论（1915 年）。狭义相对论提出光速不变原理和相对性原理，颠覆了经典力学的绝对时空观；广义相对论则将引力解释为时空弯曲的效应，成功预言了黑洞、引力透镜等现象。\n",
      "📌 内容: 海豚是齿鲸亚目海豚科哺乳动物，广泛分布于全球各大洋。它们具有极高的智商和复杂的社交行为，通过回声定位导航和捕猎，主要以鱼类、乌贼为食。海豚常被视为友好的象征，也是海洋公园中的热门动物。\n"
     ]
    }
   ],
   "source": [
    "retriever = db.as_retriever(\n",
    "search_type=\"mmr\",\n",
    "# search_kwargs={\"fetch_k\":2}\n",
    ")\n",
    "docs = retriever.invoke(\"经济政策\")\n",
    "print(len(docs))\n",
    "for doc in docs:\n",
    "    print(f\"📌 内容: {doc.page_content}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ea2b47b",
   "metadata": {},
   "source": [
    "# 保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "511308a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "folder_path = \"asset/vector/faiss\"\n",
    "db.save_local(\n",
    "    folder_path=folder_path,\n",
    "    index_name=\"index\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8d01a02",
   "metadata": {},
   "source": [
    "# 加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8f8ef99a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<langchain_community.vectorstores.faiss.FAISS at 0x7f64fca7bf10>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# allow_dangerous_deserialization=True, # 不开启会报错 ValueError: The de-serialization relies loading a pickle file. Pickle files can be modified to deliver a malicious payload\n",
    "# 原因：pickle 格式可能被篡改以植入恶意代码，存在安全风险。LangChain 为了保护用户，默认关闭了 pickle 反序列化功能，因此加载本地 Faiss 数据库时会触发 ValueError。\n",
    "\n",
    "db.load_local(\n",
    "    folder_path=folder_path,\n",
    "    embeddings=embedding_model,\n",
    "    index_name=\"index\",\n",
    "    allow_dangerous_deserialization=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eea0fba8",
   "metadata": {},
   "source": [
    "# 合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "36c69a69",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义主库文档（比如「动物类」文档）\n",
    "main_docs = [\n",
    "    Document(\n",
    "        page_content=\"海豚是高智商海洋哺乳动物，通过回声定位捕猎\",\n",
    "        metadata={\"source\": \"动物\", \"type\": \"海洋哺乳动物\"}\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"仓鼠是温顺的小型啮齿类宠物，以谷物为食\",\n",
    "        metadata={\"source\": \"动物\", \"type\": \"啮齿类哺乳动物\"}\n",
    "    )\n",
    "]\n",
    "\n",
    "# 定义目标库文档（比如「植物类」文档，要合并到主库）\n",
    "target_docs = [\n",
    "    Document(\n",
    "        page_content=\"芒果是热带水果，果肉香甜富含β-胡萝卜素\",\n",
    "        metadata={\"source\": \"水果\", \"type\": \"热带植物\"}\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"西兰花是花菜类蔬菜，富含维生素C和萝卜硫素\",\n",
    "        metadata={\"source\": \"蔬菜\", \"type\": \"花菜类植物\"}\n",
    "    )\n",
    "]\n",
    "\n",
    "# 4. 创建两个独立的 FAISS 数据库\n",
    "# 主库（动物类）\n",
    "main_db = FAISS.from_documents(main_docs, embedding_model)\n",
    "# 目标库（植物类）\n",
    "target_db = FAISS.from_documents(target_docs, embedding_model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "925b9dff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合并后的检索结果（共4条，包含动物+植物）：\n",
      "\n",
      "结果 1：\n",
      "内容：仓鼠是温顺的小型啮齿类宠物，以谷物为食\n",
      "元数据：{'source': '动物', 'type': '啮齿类哺乳动物'}\n",
      "\n",
      "结果 2：\n",
      "内容：海豚是高智商海洋哺乳动物，通过回声定位捕猎\n",
      "元数据：{'source': '动物', 'type': '海洋哺乳动物'}\n",
      "\n",
      "结果 3：\n",
      "内容：芒果是热带水果，果肉香甜富含β-胡萝卜素\n",
      "元数据：{'source': '水果', 'type': '热带植物'}\n",
      "\n",
      "结果 4：\n",
      "内容：西兰花是花菜类蔬菜，富含维生素C和萝卜硫素\n",
      "元数据：{'source': '蔬菜', 'type': '花菜类植物'}\n"
     ]
    }
   ],
   "source": [
    "# 执行合并：把 target_db（植物类）合并到 main_db（动物类）\n",
    "main_db.merge_from(target_db)\n",
    "\n",
    "# 验证合并结果：检索所有相关文档，看是否包含两类内容\n",
    "query = \"哺乳动物或植物\"\n",
    "results = main_db.similarity_search(query, k=4)\n",
    "\n",
    "print(\"合并后的检索结果（共4条，包含动物+植物）：\")\n",
    "for i, doc in enumerate(results, 1):\n",
    "    print(f\"\\n结果 {i}：\")\n",
    "    print(f\"内容：{doc.page_content}\")\n",
    "    print(f\"元数据：{doc.metadata}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7a9b1a6",
   "metadata": {},
   "source": [
    "# add_texts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "caf2482b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "新增文档的ID： ['bird_001', 'bird_002']\n",
      "新增后主库文档数： 6\n"
     ]
    }
   ],
   "source": [
    "# 要新增的文本（比如「鸟类」相关）\n",
    "new_texts = [\n",
    "    \"鹦鹉是色彩艳丽的鸟类，部分能模仿人类语言\",\n",
    "    \"企鹅是南极特有的鸟类，不会飞行但擅长游泳\"\n",
    "]\n",
    "\n",
    "# 对应的元数据（可选，和文本顺序一一对应）\n",
    "new_metadatas = [\n",
    "    {\"source\": \"动物\", \"type\": \"鸟类\"},\n",
    "    {\"source\": \"动物\", \"type\": \"南极鸟类\"}\n",
    "]\n",
    "\n",
    "# 可选：手动指定文档ID（不指定则自动生成唯一ID）\n",
    "new_ids = [\"bird_001\", \"bird_002\"]\n",
    "\n",
    "# 调用 add_texts 新增（返回新增文档的ID列表）\n",
    "added_ids = main_db.add_texts(\n",
    "    texts=new_texts,\n",
    "    metadatas=new_metadatas,\n",
    "    ids=new_ids\n",
    ")\n",
    "\n",
    "print(\"\\n新增文档的ID：\", added_ids)  # 输出：['bird_001', 'bird_002']\n",
    "print(\"新增后主库文档数：\", len(main_db.docstore._dict))  # 输出：4（2+2）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e235e592",
   "metadata": {},
   "source": [
    "# add_embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2c159825",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "昆虫类新增文档的ID： ['49264439-605f-485b-8a2d-987bcc98185c', '84867fa9-4116-419d-b3dc-9ae37e203a64']\n",
      "最终主库文档数： 8\n"
     ]
    }
   ],
   "source": [
    "# 1. 先准备「文本+嵌入」对（手动生成嵌入）\n",
    "# 要新增的文本（比如「昆虫类」相关）\n",
    "texts_to_add = [\n",
    "    \"蜜蜂是社会性昆虫，负责采集花蜜酿造蜂蜜\",\n",
    "    \"蝴蝶是完全变态昆虫，幼虫称为毛毛虫\"\n",
    "]\n",
    "\n",
    "# 2. 手动生成嵌入（复用同一个嵌入模型，也可使用其他模型）\n",
    "manual_embeddings = embedding_model.embed_documents(texts_to_add)\n",
    "\n",
    "# 3. 组合成「文本-嵌入」对（顺序必须一致）\n",
    "text_embedding_pairs = list(zip(texts_to_add, manual_embeddings))\n",
    "\n",
    "# 对应的元数据\n",
    "insect_metadatas = [\n",
    "    {\"source\": \"动物\", \"type\": \"社会性昆虫\"},\n",
    "    {\"source\": \"动物\", \"type\": \"变态昆虫\"}\n",
    "]\n",
    "\n",
    "# 调用 add_embeddings 新增\n",
    "added_insect_ids = main_db.add_embeddings(\n",
    "    text_embeddings=text_embedding_pairs,\n",
    "    metadatas=insect_metadatas\n",
    ")\n",
    "\n",
    "print(\"\\n昆虫类新增文档的ID：\", added_insect_ids)  # 输出：自动生成的唯一ID（如['xxx1', 'xxx2']）\n",
    "print(\"最终主库文档数：\", len(main_db.docstore._dict))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c5fbd8a",
   "metadata": {},
   "source": [
    "# 删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "19afec53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前所有文档ID： ['e9017c1c-0009-4803-be2d-b316f5afcb6e', '142439c3-b9dc-4e80-9795-9b31fb95243f', '75c4aaae-fa20-4cef-bb0f-cde845a55747', '80eaad42-259c-42b6-a1eb-39e89e279c1a', 'bird_001', 'bird_002', '49264439-605f-485b-8a2d-987bcc98185c', '84867fa9-4116-419d-b3dc-9ae37e203a64']\n"
     ]
    }
   ],
   "source": [
    "# 查看当前所有文档ID（通过 docstore 内部字典获取）\n",
    "all_ids = list(main_db.docstore._dict.keys())\n",
    "print(\"当前所有文档ID：\", all_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "69a7a3e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除是否成功： True\n",
      "删除后剩余文档ID： ['e9017c1c-0009-4803-be2d-b316f5afcb6e', '142439c3-b9dc-4e80-9795-9b31fb95243f', '75c4aaae-fa20-4cef-bb0f-cde845a55747', '80eaad42-259c-42b6-a1eb-39e89e279c1a', '49264439-605f-485b-8a2d-987bcc98185c', '84867fa9-4116-419d-b3dc-9ae37e203a64']\n"
     ]
    }
   ],
   "source": [
    "# 要删除的文档ID列表\n",
    "ids_to_delete = [\"bird_001\", \"bird_002\"]\n",
    "\n",
    "# 调用 delete 方法删除\n",
    "delete_success = main_db.delete(ids=ids_to_delete)\n",
    "\n",
    "print(\"删除是否成功：\", delete_success)  # 输出：True\n",
    "print(\"删除后剩余文档ID：\", list(main_db.docstore._dict.keys()))\n",
    "# 输出示例：['bird_001', 'auto_123', 'doc_0', 'doc_1']（少了2条）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain-course (3.10.12)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
